package com.acme.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.acme.dao.ClaimDAO;
import com.acme.entity.Claim;

@Transactional
@Controller
public class ClaimsController {

	@Autowired
	private ClaimDAO claimDAO;

	@RequestMapping(method = RequestMethod.GET)
	public String claim(Map<String, Object> model){
		return "Claim";

	}

	@RequestMapping(value= "downloadPDF")
	public ModelAndView downloadExcel(HttpServletRequest request){
		
		Claim claim = null;
		if(request.getSession().getAttribute("claim") != null) {
			claim = (Claim) request.getSession().getAttribute("claim");
			request.getSession().setAttribute("claim", null);
			return new ModelAndView("pdfView", "listBooks", claim);
		} else {
			int claimNum =  1;
			if (request.getParameter("viewClaimBtn") != null) {
				claimNum = Integer.parseInt(request.getParameter("viewClaimBtn"));
				request.getSession().setAttribute("claimNum", claimNum);
			} else if(request.getParameter("claimNum") != null) {
				claimNum = (Integer) request.getSession().getAttribute("claimNum");
			}

			claim = claimDAO.getClaimByID(claimNum);

			request.getSession().setAttribute("claim", null);
			return new ModelAndView("pdfView", "listBooks", claim);
		}
	}
}
